package com.engr.springbooteasypoi.thread;

import java.util.concurrent.Phaser;

/**
 * @author : liuYanXin
 * @Description : J.U.C
 * @Date : 2020-03-31
 */
public class PhaserCls {

    public static void main(String[] args) {

        Phaser phaser = new Phaser(5);
        for (int i = 0; i < 5; i++) {
            TaskCls taskCls = new TaskCls(phaser);
            Thread thread = new Thread(taskCls, "PhaserTest_" + i);
        }
    }
}

class TaskCls implements Runnable {

    private final Phaser phase;

    public TaskCls(Phaser phase) {
        this.phase = phase;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "执行任务完成，等待其他任务执行......");
        //等待其他任务执行完成
        phase.arriveAndAwaitAdvance();
        System.out.println(Thread.currentThread().getName() + "继续执行任务...");
    }
}
